package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hd.crdao.R;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/6/19 0019
 */

@SuppressLint("SetJavaScriptEnabled")
public class CarDetailsActivity extends Activity {


    public String carinfo;
    public Runnable mAction;
    public ProviderForJavaScript mProviderForJavaScript;
    public WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardetails);
        initView();
    }



    private void initView() {
        initData();
        // carinfo = getIntent().getStringExtra("carinfo");
        mWebView = findViewById(R.id.webview);
        setSettings(mWebView);
        mWebView.loadUrl("file:///android_asset/html/car_detail.html");
        mProviderForJavaScript = new ProviderForJavaScript();
        mWebView.addJavascriptInterface(mProviderForJavaScript, "connectBridge");

    }

    private void initData() {
        String carid = getIntent().getStringExtra("carid");
        HashMap<String, String> map = new HashMap<>();
        map.put("appFlag", "true");
        map.put("sign", Constants.SIGN);
        map.put("id", carid);
        OkHttpUtils.post().url(
                Constants.CARDETAIL_URL
        ).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                carinfo = response;
            }
        });
    }

    private void setSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
    }


    private class ProviderForJavaScript {

        @JavascriptInterface
        public void sendMethod() {
            mAction = new Runnable() {
                @Override
                public void run() {
                    mWebView.loadUrl("javascript:vm.init(" + carinfo + ")");
                }
            };
            mWebView.post(mAction);
        }

        @JavascriptInterface
        public void goBackPressed() {
            finish();

        }

        @JavascriptInterface
        public void goDivideFeild(String param) {
            Intent intent = new Intent(UIUtils.getContext(), DivideFeildActivity.class);
            intent.putExtra("param", param);
            startActivity(intent);
        }
    }


    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.removeJavascriptInterface("connectBridge");
            mWebView.removeAllViews();
            mProviderForJavaScript =null;
            if (mAction != null) {
                mWebView.removeCallbacks(mAction);
            }
            mWebView.destroy();
            mWebView = null;

        }
        super.onDestroy();
    }
}

